'/
' exit.bmx
' 
' Completely free to share, use, and do whatever with.
' Originally written 2010 by Anton Berlin.
' 
' Include this one after importing "exit.c", in your main BlitzMax source file.
' 
' This file is supposed to mimic the way C/C++ executes a program. It lets you
' use a 'main()' function, and returns an errorcode when the program terminates.
' Remember that 'main()' has to be an integer or void function for the program
' to run. A void version of main will always return 0 though. Valid integer
' types to use on the main function are:
' 
'		* byte
'		* short
'		* int
' 
' This is a proper way to use this in your main source file:
' 
'		superstrict
'		
'		import "exit.c"
'		include "exit.bmx"
'		
'		execute();
'		
'		function main:int()
'		
'			return 0;
'		
'		end function
'/

extern "c"

	function exit_(status:int) = "exit"

end extern

'// ----------------------------------------------------------------------------------------------------------------
'// Execute 'main()' and terminate the program using the value returned from it as an errorcode. If no 'main()'
'// function is found, the devil has been tampering with your application, and the program is terminated with the
'// errorcode 666.
'// ----------------------------------------------------------------------------------------------------------------
function execute()

	if (main) then
	
		local exitValue:int = main();
		GCCollect();
		exit_(exitValue);
	
	else
	
		exit_(666);
	
	end if

end function
